package com.ly.hotel.spainterface.jxd.dialog;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.MyLogsManager;
import com.ly.hotel.msgevent.SendLogEvent;
import com.ly.hotel.spainterface.jg.view.CustomToast;
import com.ly.hotel.spainterface.jxd.bean.MachineBean;
import com.ly.hotel.spainterface.jxd.bean.Massage;
import com.ly.hotel.spainterface.jxd.bean.PluginBean;
import com.ly.hotel.spainterface.jxd.bean.Therapy;
import com.ly.hotel.spainterface.jxd.utils.UdpUtils;
import com.ly.hotel.spainterface.jxd.utils.XmlParseUtils;
import com.ly.hotel.util.ProcessWaitingUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by ThinkPad on 2018/3/26.
 */

public class JxdSearchTherapyDialog extends DialogFragment implements View.OnClickListener {

    private Context context;
    private EditText numEditText;
    private Button buttonOne;
    private Button buttonTwo;
    private Button buttonThree;
    private Button buttonFour;
    private Button buttonFive;
    private Button buttonSix;
    private Button buttonSeven;
    private Button buttonEight;
    private Button buttonNine;
    private Button buttonZero;
    private Button buttonEmpty;
    private Button buttonSearch;
    private StringBuilder numStringBuilder;
    private PluginBean pluginBean;
    private MachineBean machineBean;
    private String isSpec = "";// 点钟
    private final int GET_THERAPY_DATA = 2,GET_HALL_ITEM = 1,GET_NO_HALL_ITEM = 0,GET_ITEM = -1;
    private final String TAG = "JxdJSActivity";
    private String artNo;
    private List<Massage> hallMassage = new ArrayList<>();
    private List<Massage> noHallMassage = new ArrayList<>();
    private List<Massage> allMassage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = MyApplication.getContext();
        numStringBuilder = new StringBuilder();
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            // 设置Dialog宽度占比为60%，高度为wrap_content
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.64), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        /*Window window = dialog.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.windowAnimations = R.style.customDialogAnim;
        window.setAttributes(layoutParams);*/
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).inflate(R.layout.jxd_search_therapy_layout,container,false);
        ImageButton searchClose = view.findViewById(R.id.search_dialog_close);
        numEditText = view.findViewById(R.id.num_edit_text);
        buttonOne = view.findViewById(R.id.button_one);
        buttonTwo = view.findViewById(R.id.button_two);
        buttonThree = view.findViewById(R.id.button_three);
        buttonFour = view.findViewById(R.id.button_four);
        buttonFive = view.findViewById(R.id.button_five);
        buttonSix = view.findViewById(R.id.button_six);
        buttonSeven = view.findViewById(R.id.button_seven);
        buttonEight = view.findViewById(R.id.button_eight);
        buttonNine = view.findViewById(R.id.button_nine);
        buttonZero = view.findViewById(R.id.button_zero);
        buttonEmpty = view.findViewById(R.id.button_empty);
        buttonSearch = view.findViewById(R.id.button_search);
        buttonOne.setOnClickListener(this);
        buttonTwo.setOnClickListener(this);
        buttonThree.setOnClickListener(this);
        buttonFour.setOnClickListener(this);
        buttonFive.setOnClickListener(this);
        buttonSix.setOnClickListener(this);
        buttonSeven.setOnClickListener(this);
        buttonEight.setOnClickListener(this);
        buttonNine.setOnClickListener(this);
        buttonZero.setOnClickListener(this);
        buttonEmpty.setOnClickListener(this);
        buttonSearch.setOnClickListener(this);
        //getHallItem();
        //getItem();

        searchClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        return view;
    }

    // 获得所有做钟项目数据
    public void getItem(){
        String req = "<gs ver=\"1.0\" function=\"桑拿做钟类型\" mode = \"request\"><MassageTypeNo>" +
                "</MassageTypeNo><MassageName></MassageName><IsHall>9</IsHall>" +
                "<OnlyMassageName>1</OnlyMassageName><SpecSetting>1</SpecSetting></gs>";
        UdpUtils udpUtils = new UdpUtils();
        udpUtils.getUdpResult(pluginBean.getIp(),pluginBean.getPort(),req,handler,GET_ITEM);
    }

    // 请求大堂项目数据
    public void getHallItem(){
        String req = "<gs ver=\"1.0\" function=\"桑拿做钟类型\" mode = \"request\"><MassageTypeNo>" +
                "</MassageTypeNo><MassageName></MassageName><IsHall>1</IsHall><OnlyMassageName>0</OnlyMassageName>" +
                "<SpecSetting>0</SpecSetting></gs>";
        UdpUtils udpUtils = new UdpUtils();
        udpUtils.getUdpResult(pluginBean.getIp(),pluginBean.getPort(),req,handler,GET_HALL_ITEM);
    }

    // 请求全身项目数据
    public void getNoHallItem(){
        String req = "<gs ver=\"1.0\" function=\"桑拿做钟类型\" mode = \"request\"><MassageTypeNo>" +
                "</MassageTypeNo><MassageName></MassageName><IsHall>0</IsHall><OnlyMassageName>0</OnlyMassageName>" +
                "<SpecSetting>0</SpecSetting></gs>";
        UdpUtils udpUtils = new UdpUtils();
        udpUtils.getUdpResult(pluginBean.getIp(),pluginBean.getPort(),req,handler,GET_NO_HALL_ITEM);
    }

    // 请求技师数据
    public void getTherapyData(){
        ProcessWaitingUtil.showProcessDialog(context,getResources().getString(R.string.loading_tips),10000);
        String req = "<gs ver=\"1.0\" function=\"技师资料查询\" mode = \"request\"><ArtNo>" + artNo + "</ArtNo><Status>" +
                "</Status><Onduty></Onduty><ArtModeNo></ArtModeNo><Columns></Columns><ArtTypeNo>" +
                "</ArtTypeNo></gs>";
        UdpUtils udpUtils = new UdpUtils();
        udpUtils.getUdpResult(pluginBean.getIp(),pluginBean.getPort(),req,handler,GET_THERAPY_DATA);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_one:
                numStringBuilder.append(buttonOne.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_two:
                numStringBuilder.append(buttonTwo.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_three:
                numStringBuilder.append(buttonThree.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_four:
                numStringBuilder.append(buttonFour.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_five:
                numStringBuilder.append(buttonFive.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_six:
                numStringBuilder.append(buttonSix.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_seven:
                numStringBuilder.append(buttonSeven.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_eight:
                numStringBuilder.append(buttonEight.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_nine:
                numStringBuilder.append(buttonNine.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_zero:
                numStringBuilder.append(buttonZero.getText().toString());
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_empty:
                numStringBuilder.setLength(0);
                numEditText.setText(numStringBuilder.toString());
                break;

            case R.id.button_search:
                artNo = numStringBuilder.toString();
                if(TextUtils.isEmpty(artNo)){
                    CustomToast customToast = CustomToast.makeText(context,getResources().getString(R.string.input_art_no), Toast.LENGTH_SHORT,false);
                    customToast.setGravity(Gravity.CENTER,0,0);
                    customToast.show();
                }else{
                    getTherapyData();
                }
                break;

            default:
                break;
        }
    }

    private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    Log.i(TAG,"接口返回：" + msg.obj.toString());
                    String resultString = msg.obj.toString();
                    if(msg.arg1 == GET_THERAPY_DATA){
                        if(resultString.startsWith("Msg") || TextUtils.isEmpty(resultString)){
                            if(TextUtils.isEmpty(resultString)){
                                CustomToast customToast = CustomToast.makeText(context,getResources().getString(R.string.connect_err), Toast.LENGTH_SHORT,false);
                                customToast.setGravity(Gravity.CENTER,0,0);
                                customToast.show();
                                numStringBuilder.setLength(0);
                                numEditText.setText(numStringBuilder.toString());
                            }else{
                                CustomToast customToast = CustomToast.makeText(context,getResources().getString(R.string.no_this_technician), Toast.LENGTH_SHORT,false);
                                customToast.setGravity(Gravity.CENTER,0,0);
                                customToast.show();
                                numStringBuilder.setLength(0);
                                numEditText.setText(numStringBuilder.toString());
                            }
                        }else{
                            List<Therapy> therapies = XmlParseUtils.getInstance().parseTherapy(resultString);
                            Therapy therapy = null;
                            String isHall = "否";
                            if(therapies.size() > 0){
                                therapy = therapies.get(0);
                            }

                            if(hallMassage != null && hallMassage.size() > 0){
                                for(int i = 0;i < hallMassage.size();i ++){
                                    Massage massage = hallMassage.get(i);
                                    String massageName = massage.getMassagename();
                                    if(therapy != null){
                                        if(therapy.getArtModeNo().equals(massageName) && massage.getIshall().equals("是")){
                                            isHall = "是";
                                            break;
                                        }
                                    }
                                }
                            }

                            if(noHallMassage != null && noHallMassage.size() > 0){
                                for(int i = 0;i < noHallMassage.size();i ++){
                                    Massage massage = noHallMassage.get(i);
                                    String massageName = massage.getMassagename();
                                    if(therapy != null){
                                        if(therapy.getArtModeNo().equals(massageName) && massage.getIshall().equals("否")){
                                            isHall = "否";
                                            break;
                                        }
                                    }
                                }
                            }

                            if(therapy != null){
                                // 跳转技师详情页面
                                JxdJSDetailDialog detailDialog = new JxdJSDetailDialog();
                                detailDialog.setPluginBean(pluginBean);
                                detailDialog.setMachineBean(machineBean);
                                detailDialog.setArtNo(therapy.getArtNo());
                                detailDialog.setTherapies(therapies);
                                detailDialog.setArrangeTimeOut(pluginBean.getArrangeTimeOut());
                                detailDialog.setIsHall(isHall);
                                detailDialog.setCancelable(false);
                                detailDialog.show(getFragmentManager(),"JxdJSDetailDialog");
                                EventBus.getDefault().post(new SendLogEvent(MyLogsManager.CLOUD_LOGS_JISHI + "\t" +
                                        MyApplication.interfaceVersion + "\t" + "查询技师" + "\t" + therapy.getArtNo()));
                                dismiss();
                            }
                        }
                        ProcessWaitingUtil.closeProcessDialog();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    public void setPluginBean(PluginBean pluginBean) {
        this.pluginBean = pluginBean;
    }

    public void setMachineBean(MachineBean machineBean) {
        this.machineBean = machineBean;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setHallMassage(List<Massage> hallMassage) {
        this.hallMassage = hallMassage;
    }

    public void setNoHallMassage(List<Massage> noHallMassage) {
        this.noHallMassage = noHallMassage;
    }
}
